﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace KinectGame
{
    public class Options
    {
        [XmlIgnore]
        private static Options _instance; 
        [XmlElement("person_name")]
        public String personName { get; set; }
        [XmlElement("board_size")]
        public int boardSize { get; set; }

        private Options()
        {
        }

        public static Options Instance
        {
            get
            {
                if (_instance == null)
                    _instance = Options.DeSerialize();
                return _instance;
            }
        }

        public static void Serialize(Options options)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Options));

            string[] strings = { " " };
            strings = DateTime.Now.Date.ToString().Split(strings, StringSplitOptions.RemoveEmptyEntries);

            if (File.Exists("../../Resources/game_options.xml")) File.Delete("../../Resources/game_options.xml");

            FileStream fs = new FileStream("../../Resources/game_options.xml", FileMode.CreateNew, FileAccess.Write);

            try
            {
                xs.Serialize(fs, options);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs.Dispose();
                }
            }
        }

        private static Options DeSerialize()
        {
            XmlSerializer xs = new XmlSerializer(typeof(Options));
            FileStream fs = new FileStream("../../Resources/game_options.xml", FileMode.Open, FileAccess.Read);

            try
            {
                return (Options)xs.Deserialize(fs);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs.Dispose();
                }
            }
        }
    }
}
